/**
 * PictureBackgroundButtonField.java
 *
 * Copyright ?1998-2008 Research In Motion Ltd.
 * 
 * Note: For the sake of simplicity, this sample application may not leverage
 * resource bundles and resource strings.  However, it is STRONGLY recommended
 * that application developers make use of the localization features available
 * within the BlackBerry development platform to ensure a seamless application
 * experience across a variety of languages and geographies.  For more information
 * on localizing your application, please refer to the BlackBerry Java Development
 * Environment Development Guide associated with this release.
 */

package oh.june.kwon.util;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Ui;

/**
 * Custom button field that shows how to use images as button backgrounds.
 */
public class ImageButton extends Field {
	private String _label;
	private int _labelHeight;
	private int _labelWidth;
	private Font _font;

	protected Bitmap _currentPicture;
	private Bitmap _onPicture;
	private Bitmap _offPicture;
	private boolean _bDrawText;
	
	
	private String _normalImage;
	private String _overImage;

	/**
	 * Constructor.
	 * 
	 * @param text
	 *            - the text to be displayed on the button
	 * @param style
	 *            - combination of field style bits to specify display
	 *            attributes
	 */
	public ImageButton(String label, String normalImage, String overImage,
			int width, int height,long style) {
		super(style);
		_normalImage = normalImage;
		_overImage = overImage;
		_offPicture = Bitmap.getBitmapResource(normalImage);
		_onPicture = Bitmap.getBitmapResource(overImage);
		
		_font = getFontEx();
		_label = label;
		_labelHeight = height;
		_labelWidth = width;
		_currentPicture = _offPicture;
	}
	
	public ImageButton(String label, String normalImage, String overImage, long style) {
		super(style);
		_normalImage = normalImage;
		_overImage = overImage;
		_offPicture = Bitmap.getBitmapResource(normalImage);
		_onPicture = Bitmap.getBitmapResource(overImage);
		
		_font = getFontEx();
		_label = label;
		_labelHeight = _offPicture.getHeight();
		_labelWidth = _offPicture.getWidth();
		_currentPicture = _offPicture;
	}
	
	public ImageButton(String label, String normalImage, String overImage,
			int width, int height,boolean bText, long style) {
		super(style);
		_normalImage = normalImage;
		_overImage = overImage;
		_offPicture = Bitmap.getBitmapResource(normalImage);
		_onPicture = Bitmap.getBitmapResource(overImage);
		
		_bDrawText = bText;
		_font = getFontEx();
		_label = label;
		_labelHeight = height;
		_labelWidth = width;
		_currentPicture = _offPicture;
	}
	
	private Font getFontEx(){
		return getFont().derive(Font.BOLD, 20, Ui.UNITS_px, Font.ANTIALIAS_STANDARD, 0);	
	}
	
	public void setNormalImage(String normalImage)
	{
		if(_normalImage.equals(normalImage))
			return;
		
		_normalImage = normalImage;
		_offPicture = Bitmap.getBitmapResource(normalImage);
		if(false == this.isFocus())
			_currentPicture = _offPicture;
		
		this.invalidate();
	}
	
	public void setOverImage(String overImage){
		if(_overImage.equals(overImage))
			return;
		
		_overImage = overImage;
		_onPicture = Bitmap.getBitmapResource(overImage);
		if( this.isFocus())
			_currentPicture = _onPicture;
		this.invalidate();
	}
	
	public void setSize(int width, int height) {
		_labelWidth = width;
		_labelHeight = height;
	}
	
	/**
	 * @return The text on the button
	 */
	public String getLabel() {
		return _label;
	}

	public void setLabel(String label){
		_label = label;
		this.invalidate();
	}
	
	/**
	 * Field implementation.
	 * 
	 * @see net.rim.device.api.ui.Field#getPreferredHeight()
	 */
	public int getPreferredHeight() {
		return _labelHeight;
	}

	/**
	 * Field implementation.
	 * 
	 * @see net.rim.device.api.ui.Field#getPreferredWidth()
	 */
	public int getPreferredWidth() {
		return _labelWidth;
	}

	/**
	 * Field implementation. Changes the picture when focus is gained.
	 * 
	 * @see net.rim.device.api.ui.Field#onFocus(int)
	 */
	protected void onFocus(int direction) {
		_currentPicture = _onPicture;
		
		invalidate();		
		super.onFocus(direction);
	}
	
	protected void setDrawText(boolean bDrawText){
		_bDrawText = bDrawText;
	}

	/**
	 * Field implementation. Changes picture back when focus is lost.
	 * 
	 * @see net.rim.device.api.ui.Field#onUnfocus()
	 */
	protected void onUnfocus() {
		_currentPicture = _offPicture;
		invalidate();
		
		super.onUnfocus();
	}
	
	/**
	 * Field implementation.
	 * 
	 * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
	 */
	protected void drawFocus(Graphics graphics, boolean on) {
		// Do nothing
	}

	/**
	 * Field implementation.
	 * 
	 * @see net.rim.device.api.ui.Field#layout(int, int)
	 */
	protected void layout(int width, int height) {
		setExtent(Math.min(width, getPreferredWidth()), Math.min(height,
				getPreferredHeight()));
	}

	/**
	 * Field implementation.
	 * 
	 * @see net.rim.device.api.ui.Field#paint(Graphics)
	 */
	protected void paint(Graphics graphics) {
		// First draw the background colour and picture

		graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0,
				0);
		
		
		if(_bDrawText)
		{
			// Then draw the text graphics.setColor(Color.BLACK);
			graphics.setColor(Color.WHITE);
			graphics.setFont(_font);
			
			graphics.drawText(_label, 4, 10, (int)(getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ), getWidth()- 6 );
		}
		
	}

	/**
	 * Overridden so that the Event Dispatch thread can catch this event instead
	 * of having it be caught here..
	 * 
	 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
	 */
	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(1);
		return true;
	}


	protected boolean navigationMovement(int dx,
            int dy,
            int status,
            int time)
	{
		invalidate();
		return false; 
	}
}
